<template>
  <div class>
    <el-card class="btns">
      <el-button size="medium" type="primary" @click="submitForm('ruleForm')">查询</el-button>
      <el-button size="medium" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="medium" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </el-card>
    <el-card style="position:relative;">
      <el-form :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px" class="demo-ruleForm">
        <div class="icon-arrow" v-if="folding">
          <i @click="offForm=!offForm" :title="offForm?'展开表单':'收起表单'" :class="offForm?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
        </div>
        <el-form-item label="桌面名称" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="桌面编码" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
        </el-form-item>

      </el-form>
    </el-card>
    <el-table stripe :data="tableData" border style="width: 100%" size="mini">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      <el-table-column align="center" label="桌面名称">
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="桌面编码">
        <template slot-scope="scope">
          <span>{{scope.row.code}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="X轴">
        <template slot-scope="scope">
          <span>{{scope.row.xAxis}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Y轴">
        <template slot-scope="scope">
          <span>{{scope.row.yAxis}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="高度">
        <template slot-scope="scope">
          <span>{{scope.row.hAxis}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="宽度">
        <template slot-scope="scope">
          <span>{{scope.row.wAxis}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="组件名称">
        <template slot-scope="scope">
          <span>{{scope.row.zjName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增管理" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form" ref="addForm" :rules="rules" size="small">
        <el-form-item label="桌面名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="桌面编码" :label-width="formLabelWidth" prop="code">
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="X轴" :label-width="formLabelWidth" prop="xAxis">
          <el-input v-model="form.xAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Y轴" :label-width="formLabelWidth" prop="yAxis">
          <el-input v-model="form.yAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="高度" :label-width="formLabelWidth" prop="hAxis">
          <el-input v-model="form.hAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宽度" :label-width="formLabelWidth" prop="wAxis">
          <el-input v-model="form.wAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件名称" :label-width="formLabelWidth" prop="zjName">
          <el-input v-model="form.zjName" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="组件图表" :label-width="formLabelWidth" prop="zjIcon">
          <el-input v-model="form.zjIcon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------编辑弹出框 -->
    <el-dialog title="编辑管理" :visible.sync="dialogEditVisible" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="桌面名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="桌面编码" :label-width="formLabelWidth" prop="code">
          <el-input v-model="editForm.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="X轴" :label-width="formLabelWidth" prop="xAxis">
          <el-input v-model="editForm.xAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Y轴" :label-width="formLabelWidth" prop="yAxis">
          <el-input v-model="editForm.yAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="高度" :label-width="formLabelWidth" prop="hAxis">
          <el-input v-model="editForm.hAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="宽度" :label-width="formLabelWidth" prop="wAxis">
          <el-input v-model="editForm.wAxis" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件名称" :label-width="formLabelWidth" prop="zjName">
          <el-input v-model="editForm.zjName" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="组件图表" :label-width="formLabelWidth" prop="zjIcon">
          <el-input v-model="editForm.zjIcon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      dialogEditVisible: false,
      offForm: true, //是否折叠
      folding: false, //表单是否超过一行
      formLabelWidth: "80px",
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      ruleForm: {
        title: "",
        code: ""
      },
      style: {},
      rules: {},
      form: {
        title: "",
        code: "",
        xAxis: "",
        yAxis: "",
        hAxis: "",
        wAxis: "",
        zjName: "",
        zjIcon:""
      },
      editForm: {
        id: "",
        title: "",
        code: "",
        xAxis: "",
        yAxis: "",
        hAxis: "",
        wAxis: "",
        zjName: "",
        zjIcon:""
      }
    };
  },

  mounted() {
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px",
        overflow: "hidden"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto",
        overflow: "hidden"
      };
    }
  },
  computed: {},
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    }
  },
  methods: {
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.ruleForm
      };
      this.$api
        .formPost(  "/api/biz/potal/potSet/doGetList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    showDialog() {
      this.dialogFormVisible = true;
      this.form = {};
    },
    showEditDialog(index, row) {
      console.log(row);
      this.editForm.id = row.id;
      this.editForm.title = row.title;
      this.editForm.code = row.code;
      this.editForm.xAxis = row.xAxis;
      this.editForm.yAxis = row.yAxis;
      this.editForm.hAxis = row.hAxis;
      this.editForm.wAxis = row.wAxis;
      this.editForm.zjName = row.zjName;
       this.editForm.zjIcon = row.zjIcon;
      this.dialogEditVisible = true;
    },
    handleAdd() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$api
            .formPost(  "/api/biz/potal/potSet/doSave", this.form)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "添加成功！",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.init();
              } else {
                this.$message({
                  message: "添加失败！",
                  type: "error"
                });
              }
            });
        }
      });
    },
    handleEdit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let params = this.editForm;
          this.$api
            .formPost(  "/api/biz/potal/potSet/doModById", params)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message.success("执行成功！");
                this.init();
                this.dialogEditVisible = false;
              } else {
                this.$message.error("执行失败！");
              }
            });
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost(  "/api/biz/potal/potSet/doRmById", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.init();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
.icon-arrow {
  position: absolute;
  bottom: 5px;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #f1f1f1;
  i {
    background-color: #fff;
    position: absolute;
    color: #999;
    bottom: -7px;
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
}
</style>